# -*- coding: utf-8 -*-
"""
Created on Wed May 20 01:05:18 2020

@author: HP
"""

import os.path
import pandas as pd
from garp import garp_find_e
from PermTester import PermTester

df = pd.read_excel('FismanKM-giving3person.xls')
#Make prices
df['p_Hold'] = 100. / df['Max-Hold']
df['p_Pass-A'] = 100. / df['Max-A']
df['p_Pass-B'] = 100. / df['Max-B']

#If the results file doesn't exist then create it.
file_results = 'FismanKM-giving3person-perm.xlsx'

columns = [ 'panelist', 'periods', 'avg pass share', 
           'e-score', 'p-value']

if not os.path.isfile(file_results):
    df_results = pd.DataFrame( columns = columns )
    df_results.to_excel(file_results)

df_results = pd.read_excel(file_results,index_col = 0)

ids = list( set( df['ID'] ))

for panelistid in ids:
    #Make sure we have not processed already.    
    if not any( df_results['panelist'] == panelistid ):
        print('panelist {}'.format(panelistid))
        
        df_cur = df[ df['ID'] == panelistid ].copy()
        df_cur['pass value'] = \
            df_cur['Pass-A'] * df_cur['p_Pass-A'] + \
            df_cur['Pass-B'] * df_cur['p_Pass-B']
        df_cur['pass share'] = \
            ( df_cur['pass value'] ) / \
            ( df_cur['pass value'] + df_cur['Hold'] * df_cur['Max-Hold'] )
        C = df_cur[ ['Hold','Pass-A','Pass-B'] ].to_numpy()
        P = df_cur[ ['p_Hold','p_Pass-A','p_Pass-B'] ].to_numpy()
        
        avg_pass_share = df_cur['pass share'].mean()
        e_score  = garp_find_e(C,P)
        pt = PermTester(C,P)
        p_value = pt('garp','r')
        
        result_cur = [ panelistid, C.shape[0], avg_pass_share, e_score, p_value ]
        result_cur_dict = dict( zip(columns,result_cur) )
        df_result_cur = pd.DataFrame( result_cur_dict, index = [panelistid] )
        df_results = df_results.append(df_result_cur)
        df_results.to_excel(file_results)


